#include "cgal_source_edge.h"
#include <vector>
#include <vtkABI.h>
#include <vtkCell.h>
#include <vtkCellArray.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkType.h>

namespace cgalView {
vtkTypeBool cgal_source_edge::RequestData(vtkInformation *request,
                                          vtkInformationVector **inInfoVector,
                                          vtkInformationVector *outInfoVector) {
  if (!d_mesh) {
    return false;
  }

  if (!d_mesh->number_of_edges()) {
    return false;
  }

  auto output = vtkPolyData::GetData(outInfoVector);
  if (!output) {
    return false;
  }

  auto points = vtkPoints::New();
  for (auto const &p : d_mesh->points()) {
    points->InsertNextPoint(p.x(), p.y(), p.z());
  }

  output->SetPoints(points);

  auto lines = vtkCellArray::New();
  for (auto e : d_mesh->edges()) {
    std::vector<vtkIdType> rline{};
    rline.push_back(d_mesh->source(d_mesh->halfedge(e)));
    rline.push_back(d_mesh->target(d_mesh->halfedge(e)));
    lines->InsertNextCell(2, rline.data());
  }
  output->SetLines(lines);

  return true;
}
} // namespace cgalView